package com.zkingsoft.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.dao.BizUserDao;
import com.zkingsoft.service.BizUserService;

/**
 * @description service接口实现类(用户表)
 * @author 何明
 * @date 2018-05-23 00:08
 */
@Service
public class  BizUserServiceImpl implements  BizUserService{

	
	@Autowired
	private BizUserDao bizUserDao;
	
	@Value("${tempAvatarUrl}")
	private String tempAvatarUrl;
	
	@Override
	public int add(BizUser bizUser){
		// 设置基本字段信息，临时字段
		bizUser.setUserIsAuthorize(AppConstance.IS_NOT_AUTHORIZE);
		bizUser.setCreateBy(AppConstance.USER_TYPE_ADMIN);
		bizUser.setUpdateBy(AppConstance.USER_TYPE_ADMIN);
		bizUser.setUserId(UUIDUtil.getRandomID());
		return bizUserDao.insert(bizUser);
		
	}
	
	@Override
	public int batchAdd(List<BizUser>  bizUserList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizUserList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizUserList.size() ? bizUserList.size() : end;
			List<BizUser> insertList = bizUserList.subList(begin, end);
			num += bizUserDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizUser oldBizUser
	,BizUser newBizUser){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizUser, newBizUser)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizUser, newBizUser);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizUser);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("userId", oldBizUser.getUserId());
			bizUserDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizUser bizUser){
	
		return bizUserDao.updateByModel(bizUser);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizUserDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String userId){
	
		return bizUserDao.deleteById(userId);
	
	}
	
	@Override
	public int removeByModel(BizUser bizUser){
	
		return bizUserDao.deleteByModel(bizUser);
	
	}
	
	
	@Override
	public List<BizUser> findInPage(BizUser bizUser,  PaginationVO pageVo){
	
		return bizUserDao.selectInPage(bizUser , pageVo);
	
	}
	
	@Override
	public List<BizUser> findByModel(BizUser bizUser){
	
		return bizUserDao.selectByModel(bizUser);
	
	}
	
	@Override
	public int  findTotal(BizUser bizUser){
	
		return bizUserDao.selectTotalRecord(bizUser);
	
	}
	
	@Override
	public BizUser  findById(String userId){
	
		return bizUserDao.selectById(userId);
	
	}

	@Override
	public int modifyByOpenId(BizUser bizUser) {
		return bizUserDao.updateByOpenId(bizUser);
	}

	
	@Override
	public BizUser findByOpenId(String openId) {
		return bizUserDao.findByOpenId(openId);
	}
	
	/**
	 * 保存用户信息
	 */
	@Override
	public int saveUserInfo(BizUser bizUser) {
			
		return bizUserDao.updateByModel(bizUser);
	}
   	
	
	
}